module func_test; 
use grid_mod;
use func_factory;
implicit none;

contains 

logical function func_test_all() result(ok);  implicit none; 
  call dbg_info("-->func_drv_test_all()"); 
  ok = .TRUE.
  ok = func_polynom_test();
  !ok = vec_dot_test_speed();
  return
end function

logical function func_polynom_test() result(ok);  implicit none; 
  integer :: vsize;
  real(R14) :: minv, maxv, res;
  real(R14), dimension(:), pointer :: px, py;      
  real(R14), dimension(3) :: coeff;      
  call dbg_info("-->new_func_drv_test()"); 
  ok = .TRUE.

  vsize = 3;  minv = D01; maxv = 1.1D0;
  px => new_step_grid(minv, maxv, vsize);   call dbg_vec("px=", px);
  call assert_max_err("3.px(1)=minv", px(1) - minv, ERR14);
  call assert_max_err("3.px(2)=minv", px(2) - (maxv+minv)/2., ERR14);
  call assert_max_err("3.px(vsize)=maxv", px(vsize) - maxv, ERR14);
  
  coeff = (/D1, D0, D0/);
  py => new_func_polynom(px, coeff);  call dbg_vec("py=", py);
  res = D1;
  call assert_max_diff("1.py(1)", py(1), res, ERR14);
  call assert_max_diff("1.py(2)", py(2), res, ERR14);
  call assert_max_diff("1.py(3)", py(3), res, ERR14);
  call del_vec(py);

  coeff = (/D0, D05, D0/);
  py => new_func_polynom(px, coeff); call dbg_vec("py=", py);
  res = 0.5D0;
  call assert_max_diff("05.py(1)", py(1), res * px(1), ERR14);
  call assert_max_diff("05.py(2)", py(2), res * px(2), ERR14);
  call assert_max_diff("05.py(3)", py(3), res * px(3), ERR14);
  call del_vec(py);

  coeff = (/-D05, -D1, D3/);
  py => new_func_polynom(px, coeff); call dbg_vec("py=", py);
  res = -0.5D0;
  call assert_max_diff("513(1)", py(1),res-px(1)+3*px(1)*px(1),ERR14);
  call assert_max_diff("513(2)", py(2),res-px(2)+3*px(2)*px(2),ERR14);
  call assert_max_diff("513(3)", py(3),res-px(3)+3*px(3)*px(3),ERR14);
  call del_vec(py);

  call del_vec(px);
  return
end function

end module 